package dashboard

import (
	model "aggregation/model/dashboard"
	"aggregation/store"
	"aggregation/store/postgres"
	"aggregation/store/redis"
	"context"
	"time"
)

type market struct {
	UserIncrease      store.ICounter
	UserIncreaseCache store.ICache
	UserBind          store.ICounter
	UserBindCache     store.ICache
	UserUnbind        store.ICounter
	UserUnbindCache   store.ICache
	ShopIncrease      store.ICounter
	ShopIncreaseCache store.ICache
}

var Market = &market{
	UserIncrease:      postgres.UserCount,
	UserIncreaseCache: redis.UserIncrease,
	UserBind:          postgres.UserBindBike,
	UserBindCache:     redis.UserBindBikeCache,
	UserUnbind:        postgres.UserUnbindBike,
	UserUnbindCache:   redis.UnbindCache,
	ShopIncrease:      postgres.Shop,
	ShopIncreaseCache: redis.Shop,
}

// 暂时填写加数据，后续添加逻辑

func (m *market) MarketDashboard(ctx context.Context) (model.MarketBoardResponse, error) {
	var (
		dashboard model.MarketBoardResponse
	)
	storeSold, _ := postgres.Market.SoldBike(ctx)
	unbindBike, _ := Device.Unbind.Count(ctx)
	bindBike, _ := Device.Bind.Count(ctx)
	bindUser, _ := User.UserBindBike.Count(ctx)
	unbindUser, _ := User.UserUnBindBike.Count(ctx)
	distribute, _ := postgres.Market.Distribute(ctx)
	UnDistribute, _ := postgres.Market.UnDistribute(ctx)
	dashboard = model.MarketBoardResponse{
		StoreSold:      storeSold,
		NotDistribute:  UnDistribute,
		Distribute:     distribute,
		StoreWarehouse: 0,
		BindBike:       bindBike,
		UnbindBike:     unbindBike,
		BindUser:       bindUser,
		UnbindUser:     unbindUser,
	}
	return dashboard, nil
}

func (m *market) SaleRank(ctx context.Context) (model.StoreRankResponse, error) {
	var (
		rank model.StoreRankResponse
	)
	r, _ := postgres.Market.StoreRank(ctx)
	rank = model.StoreRankResponse{
		Rank: r,
	}
	return rank, nil
}

// 上个月新增用户
func (m *market) UserRideLastmonth(ctx context.Context) (model.RideLastMonthResponse, error) {
	var (
		ride model.RideLastMonthResponse
	)
	month := monthIncrease(ctx, time.Now(), m.UserIncreaseCache, m.UserIncrease)
	ride = model.RideLastMonthResponse{
		RideLastMonth: month,
	}
	return ride, nil
}

// UserRideWave 这里其实是新增用户，用户页面的图一致
func (m *market) UserRideWave(ctx context.Context, period model.PERIOD) (model.RideWaveResponse, error) {
	var (
		wave model.RideWaveResponse
	)
	if period == model.DAY {
		wave = model.RideWaveResponse{
			Period:         period,
			UnbindUserRide: dayUserWave(ctx, time.Now(), m.UserUnbindCache, m.UserUnbind),
			BindUserRide:   dayUserWave(ctx, time.Now(), m.UserBindCache, m.UserBind),
		}
	} else {
		wave = model.RideWaveResponse{
			Period:         period,
			UnbindUserRide: monthUserWave(ctx, time.Now(), m.UserUnbindCache, m.UserUnbind),
			BindUserRide:   monthUserWave(ctx, time.Now(), m.UserBindCache, m.UserBind),
		}
	}
	return wave, nil
}

// StoreIncreaseBoard 门店新增面板
func (m *market) StoreIncreaseBoard(ctx context.Context) (model.StoreIncreaseBoardResponse, error) {
	var (
		increase model.StoreIncreaseBoardResponse
	)
	total, _ := postgres.Market.StoreTotal(ctx)
	month := monthIncrease(ctx, time.Now(), m.ShopIncreaseCache, m.ShopIncrease)
	increase = model.StoreIncreaseBoardResponse{
		Total:             total,
		IncreaseLastMonth: month,
	}
	return increase, nil
}

// 门店新增波浪图
func (m *market) StoreIncreaseWave(ctx context.Context, period model.PERIOD) (model.StoreIncreaseWaveResponse, error) {
	var (
		wave model.StoreIncreaseWaveResponse
	)
	switch period {
	case model.MONTH:
		wave.StoreIncrease = monthWave(ctx, time.Now(), m.ShopIncreaseCache, m.ShopIncrease)
	default:
		wave.StoreIncrease = dayWave(ctx, time.Now(), m.ShopIncreaseCache, m.ShopIncrease)
	}
	return wave, nil
}

// sim 卡收入面板
func (m *market) SimBoard(ctx context.Context) (model.SimBoardResponse, error) {
	var (
		sim model.SimBoardResponse
	)
	sim = model.SimBoardResponse{}
	return sim, nil
}

// sim卡波浪图
func (m *market) SimWave(ctx context.Context, period model.PERIOD) (model.SimWaveResponse, error) {
	var (
		sim model.SimWaveResponse
	)
	if period == model.DAY {
		sim = model.SimWaveResponse{
			Period: period,
		}
	} else {
		sim = model.SimWaveResponse{}
	}
	return sim, nil
}

func (m *market) NationStoreBike(ctx context.Context) (model.NationStoreBikeResponse, error) {
	var (
		storeBike model.NationStoreBikeResponse
	)
	storeBike = model.NationStoreBikeResponse{}
	result, _ := postgres.Market.ProvinceSummary(ctx)
	storeBike.Areas = result
	return storeBike, nil
}
